<template>
  <a-card :bordered="false">
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="mainId">
      <a-button @click="handleAdd" type="primary" icon="plus" v-show="!isChecked">新增</a-button>
      <a-button @click="handleGenerateProductOrder" type="primary" icon="plus" v-show="isChecked">下工单</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0 && !isChecked">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="small"
        bordered
        rowKey="id"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <template slot="routingFlowChart" slot-scope="text, record">
          <a @click="handleFlowChartOpen(record)">点击查看</a>
        </template>

        <template slot="materialCode" slot-scope="text, record">
          <HfMaterialSearchSelectTag
            v-if="record.selectable"
            v-model="record.materialCode"
            :material-field="['name', 'materialCode', 'materialName', 'sap']"
            :data-url="url.materialList"
            style="width:200px"
            @change="onMaterialChange(arguments, record)"
          ></HfMaterialSearchSelectTag>
          <span v-else>{{ text }}</span>
        </template>
        <template slot="planProduceNumber" slot-scope="text, record">
          <a-input-number
            v-model:value="record.planProduceNumber"
            v-if="record.selectable"
            ref="inputLabel"
            @keyup.enter="save(record)"
          ></a-input-number>
          <span v-else>{{ text }}</span>
        </template>
        <template slot="actualUsedHours" slot-scope="text, record">
          <a-input-number v-model:value="record.actualUsedHours" v-if="record.selectable"></a-input-number>
          <span v-else>{{ text }}</span>
        </template>
        <template slot="action" slot-scope="text, record">
          <div class="editable-row-operations" v-show="record.status !== 1">
            <span v-if="record.editable">
              <a @click="() => save(record)">保存</a>
              <a-divider type="vertical" style="background-color: grey"></a-divider>
              <a @click="() => cancel(record)">取消</a>
            </span>
            <span v-else>
              <a :disabled="editingKey !== ''" @click="edit(record)" v-show="!isChecked">编辑</a>
              <a-divider type="vertical" style="background-color: grey" v-show="!isChecked"></a-divider>
              <a :disabled="editingKey !== ''" @click="handleDelete(record.id)" v-show="!isChecked">删除</a>
            </span>
          </div>
        </template>
        <template slot="remark" slot-scope="text, record">
          <a-textarea
            v-model="record.remark"
            v-if="record.editable"
            style="width: 100%"
            @keyup.enter="save(record)"
          ></a-textarea>
          <span v-else>{{ text }}</span>
        </template>
      </a-table>
    </div>

    <HfShowRoutingTree ref="routingFlowChart"></HfShowRoutingTree>
    <GenerateProductOrderPreview ref="genProductOrder"></GenerateProductOrderPreview>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { HfIODetailMixin } from '@/mixins/HfIODetailMixin'
import { postAction, putAction } from '@api/manage'
import HfMaterialSearchSelectTag from '@comp/HfManual/HfMaterialSearchSelectTag.vue'
import GeneratePickingPreview from '@views/production/modules/generatePicking/GeneratePickingPreview.vue'
import GenerateProductPreview from '@views/production/modules/generateProduct/GenerateProductPreview.vue'
import HfShowRoutingTree from '@views/production/treeNode/HfShowRoutingTree.vue'
import GenerateProductOrderPreview from '@views/production/modules/generateProductOrder/GenerateProductOrderPreview.vue'

export default {
  name: 'HfProductmissionDetailList',
  mixins: [JeecgListMixin, HfIODetailMixin],
  components: {
    GenerateProductOrderPreview,
    HfShowRoutingTree,
    GenerateProductPreview,
    GeneratePickingPreview,
    HfMaterialSearchSelectTag
  },
  props: {
    mainId: {
      type: String,
      default: '',
      required: false
    },
    sourceSerial: {
      type: String,
      default: '',
      required: false
    }
  },
  watch: {
    mainId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['sourceId'] = val
          this.loadData(1)
        }
      }
    }
  },
  data() {
    return {
      description: '生产任务单管理页面',
      disableMixinCreated: true,
      isChecked: false,
      routingFlowChartOpen: false,
      openModal: false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '工艺流程图',
          align: 'center',
          dataIndex: 'routingFlowChart',
          scopedSlots: { customRender: 'routingFlowChart' }
        },
        {
          title: '工艺流程',
          align: 'center',
          dataIndex: 'routingName'
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode',
          scopedSlots: { customRender: 'materialCode' }
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '计划生产数量',
          align: 'center',
          dataIndex: 'planProduceNumber',
          scopedSlots: { customRender: 'planProduceNumber' }
        },
        // {
        //   title: '实际生产数量',
        //   align: 'center',
        //   dataIndex: 'actualProduceNumber'
        // },
        {
          title: '计划消耗工时',
          align: 'center',
          dataIndex: 'planUsedHours'
        },
        // {
        //   title: '实际消耗工时',
        //   align: 'center',
        //   dataIndex: 'actualUsedHours',
        //   scopedSlots: { customRender: 'actualUsedHours' }
        // },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType_dictText'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
          scopedSlots: { customRender: 'remark' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/production/hfProductmission/listHfProductmissionDetailByMainId',
        delete: '/production/hfProductmission/deleteHfProductmissionDetail',
        deleteBatch: '/production/hfProductmission/deleteBatchHfProductmissionDetail',
        exportXlsUrl: '/production/hfProductmission/exportHfProductmissionDetail',
        importUrl: '/production/hfProductmission/importHfProductmissionDetail',
        add: '/production/hfProductmission/addHfProductmissionDetail',
        edit: '/production/hfProductmission/editHfProductmissionDetail',
        materialList: '/production/hfRouting/queryByMaterialCodeOrName'
      },
      dictOptions: {
        productPropose: []
      }
    }
  },
  created() {},
  computed: {
    importExcelUrl() {
      return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`
    }
  },
  methods: {
    handleFlowChartOpen(record) {
      this.$refs.routingFlowChart.visible = true
      this.$refs.routingFlowChart.routingId = record.routingId
      this.$refs.routingFlowChart.mainMaterialCode = record.materialCode
      this.$refs.routingFlowChart.mainMaterialName = record.materialName
    },
    clearList() {
      this.dataSource = []
      this.selectedRowKeys = []
      this.ipagination.current = 1
    },
    onMaterialChange([, record], tableRow) {
      Object.assign(tableRow, {
        materialId: record.materialId,
        materialCode: record.materialCode,
        materialName: record.materialName,
        sap: record.sap,
        unit: record.unit,
        materialType: record.materialType,
        warehouse: record.warehouse,
        routingId: record.id,
        routingName: record.name,
        steps: record.steps
      })
    },
    edit(record) {
      this.searchMaterial()
      const newData = [...this.dataSource]
      const target = this.dataSource.find(item => item.id === record.id)
      if (target) {
        target.editable = true
        target.selectable = true
        this.selectValue = target.materialCode
        this.dataSource = newData
      }
      this.$nextTick(() => {
        this.$refs.inputLabel.focus()
      })
    },
    save(record) {
      let param = Object.assign({}, record)
      param['sourceId'] = this.mainId
      param['sourceSerial'] = this.sourceSerial
      this.loading = false
      if (param.add) {
        postAction(this.url.add, param)
          .then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = true
          })
      } else {
        putAction(this.url.edit, param)
          .then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = true
            this.selectValue = ''
          })
      }
    },

    handleGenerateProductOrder() {
      if (this.selectedRowKeys.length === 0) {
        this.$message.warning('请选择要下工单的物料')
        return
      }
      this.$refs.genProductOrder.visible = true
      this.$refs.genProductOrder.loadData(this.selectionRows)
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
